<?php
/**
 * @user:andy
 * @date:2021/6/16
 * @company:http://www.sjdphp.com
 * @desc:
 */

namespace app\common\server;


class Tools
{
    const BACK_PWD_KEY = 'ZzPHP.com';

    /** 生产随机字符串
     * @param $str
     * @param $key
     * @return string
     */
    public static function md5AdminPwd($str,$key){
        return md5($str . $key . self::BACK_PWD_KEY);
    }

    /** 校验密码
     * @param $str
     * @param $key
     * @param $pwd
     * @return bool
     */
    public static function comparePassword($str,$key,$pwd){
        if(self::md5AdminPwd($str,$key) === $pwd){
            return true;
        }else{
            return false;
        }
    }


    /** 生产随机字符串
     * @param int $length 长度
     * @return string
     */
    public static function getRandStr($length){
        //字符组合
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()';
        $len = strlen($str)-1;
        $randstr = '';
        for ($i=0;$i<$length;$i++) {
            $num=mt_rand(0,$len);
            $randstr .= $str[$num];
        }
        return $randstr;
    }
}